﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using BaseHelper;
using System.Drawing;
using System.Text.RegularExpressions;
using DeviceSDK.LED.XianKe.XianKe;
using BaseHelper.Base;


namespace DeviceSDK.LED.XianKe
{
    public class XianKe_RXTXGY
    {
        private static SortedList<string, string> slPlaybill000 = new SortedList<string, string>();
        private static SortedList<string, string> slLastMd5 = new SortedList<string, string>();
 

        public static RXTX_SDK Connect(string IP, int Port)
        {

            RXTX_SDK rxtx = new RXTX_SDK();
            if (rxtx.Connect(IP, Port))
            {
                return rxtx;
            }
            return null;
        }

    
        //发送-节目
        public static LEDSendResult Send_Programs(bool IsTempPlaybill, string IP, int Port, int Width, int Height, PlaybillProgramInfo[] Programs, bool isCheckResult=false)
        {
            RXTX_SDK rxtx = Connect(IP, Port);
            LEDSendResult result = new LEDSendResult(isCheckResult);

            if (rxtx == null)
            {
                result.RetCode = 1;
                return result;
            }
            int ret = 1;
            PlayBill pb = new PlayBill();


            foreach (PlaybillProgramInfo pinfo in Programs)
            {

                if (pinfo.Type == "picture" && !pinfo.FilePath.ToString().Contains(".bmp"))
                {
                    try
                    {
                        FileInfo finfo = new FileInfo(pinfo.FilePath);
                        string newPath = finfo.FullName.Replace(finfo.Extension, ".bmp");

                        if (ImageHelper.Bmp32To24(finfo.FullName, newPath))
                            pinfo.FilePath = newPath;
                        else
                            pinfo.FilePath = "";
                        
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Err("DeviceSDK.LED.XianKe.XianKe_RXTX.cs", "Send_Programs()", ex);
                    }
                }
            }

  
            foreach (PlaybillProgramInfo pinfo in Programs)
            {

                switch (pinfo.Type)
                {
                    case "picture":
                        pb.AddImage(pinfo.FilePath,pinfo.PlayTime,pinfo.X,pinfo.Y);
                        break;

                    case "text":
                        pb.AddString(pinfo.Text, pinfo.PlayTime, pinfo.FontName, pinfo.FontSize, pinfo.FontColor, pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height,pinfo.PositionHor,pinfo.PositionVer,"#000000",pinfo.IsEffectRand==0?1:0);
                        break;
                }
            }

            //这里是获取下原来的内容，然后与新发送的内容进行合并发送。
            //--------------临时------------------
            
            try
            {
  
                string playbill999_md5 = BaseHelper.Encrypt.Md5Helper.Encrypt(pb.Create());
               
                byte[] playbil_000 = rxtx.Download("list\\000.xkl");
                if (playbil_000 != null)
                {
                    
                    string list = Encoding.Default.GetString(playbil_000);
                    string list_md5 = BaseHelper.Encrypt.Md5Helper.Encrypt(list);


                    if ((!slPlaybill000.ContainsKey(IP) || slPlaybill000[IP].ToString() != list_md5) || (!slLastMd5.ContainsKey(IP) || slLastMd5[IP].ToString() != playbill999_md5))
                    {
       

                        Regex reg = new Regex("Item\\d{2}=(.*?)\\r\\n");
                        foreach (Match mc in reg.Matches(list))
                        {
                            pb.AddSource(mc.Groups[1].Value);
                        }
                        slPlaybill000[IP] = list_md5;
                        IsTempPlaybill = false;
                    }
                    else
                    {
       
                    }

                }

            }
            catch { }
            //-------------end---------------

            //临时插入，就一张图片
            if (IsTempPlaybill)
            {
   
                if (Programs.Length == 1 && Programs[0].Type == "picture")
                {

                    if (rxtx.UpLoad("image\\888.bmp", Programs[0].FilePath))
                        ret = 0;
                    else
                        ret = 5;

                    result.RetCode = ret;
                    return result;
                }
            }

            int playbillId = 999;// IsTempPlaybill ? 999 : 0;
   
            if (rxtx.SendPlaybill(playbillId, pb))
            {
                if (rxtx.SetPlaybill(playbillId))
                {
                    if (isCheckResult)
                    {
                        result.SendContent = pb.Create();
                        result.ReplayContent = rxtx.GetPlaybillContent(playbillId);
                    }

                    ret = 0;
                }
                else
                    ret = 1;
            }
            else
                ret = 2;

            result.RetCode = ret;
            rxtx.Close();
            return result;

        }
    }
}
